package com.demo.servlet.demo1210;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet("/demo1210/demo01.do")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("本次请求传来的所有请求头信息如下：<br/>");
        //1.获取请求消息中所有头字段
        Enumeration<String> headerNames = request.getHeaderNames();
        //2.使用循环遍历所有请求头
        while (headerNames.hasMoreElements()) {
            //遍历到每一个请求头名称
            String headerName = headerNames.nextElement();
            writer.println(headerName + "：" + request.getHeader(headerName) + "<br/>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
